<?php
// 简单的豆包AI测试脚本

// 引入配置
$config = include '../application/extra/doubao.php';
$apiKey = $config['api_key'];
$apiUrl = $config['chat_api_url'];

echo "简单豆包AI测试\n";
echo "================\n";

// 构建测试请求数据
$requestData = [
    'model' => 'doubao-1-5-pro-32k-250115',
    'messages' => [[
        'role' => 'user', 
        'content' => '你好世界'
    ]],
    'stream' => false, // 非流式输出
    'temperature' => 0.7,
    'max_tokens' => 100
];

echo "发送测试请求...\n";

// 初始化cURL
$ch = curl_init();

// 设置cURL选项
curl_setopt_array($ch, [
    CURLOPT_URL => $apiUrl,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => json_encode($requestData, JSON_UNESCAPED_UNICODE),
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json; charset=utf-8',
        'Authorization: Bearer ' . $apiKey
    ],
    CURLOPT_TIMEOUT => 30,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_SSL_VERIFYPEER => false,  // 跳过SSL证书验证
    CURLOPT_SSL_VERIFYHOST => false,  // 跳过主机验证
    CURLOPT_FOLLOWLOCATION => true
]);

// 执行请求
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);

curl_close($ch);

// 输出结果
echo "HTTP状态码: $httpCode\n";
if (!empty($error)) {
    echo "cURL错误: $error\n";
} else {
    echo "响应数据:\n$response\n";
    
    // 尝试解析JSON
    $responseData = json_decode($response, true);
    if ($responseData && isset($responseData['choices'][0]['message']['content'])) {
        echo "\nAI回复内容:\n" . $responseData['choices'][0]['message']['content'] . "\n";
    }
}